package com.wolfsea.rvitemdecorationdemo.itemdecoration
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import androidx.recyclerview.widget.RecyclerView
import com.wolfsea.rvitemdecorationdemo.R

/**
 *@desc   自定义分割线
 *@author liuliheng
 *@time 2022/3/13  19:41
 **/
class GridLayoutDecorationLine(context: Context, rowCount: Int, spanCount: Int, type: Int) : RecyclerView.ItemDecoration() {

    private val mContext = context
    private val mRowCount = rowCount
    private val mSpanCount = spanCount
    private val mType = type

    private var paint: Paint = Paint()
    private var interactPaint: Paint = Paint()

    override fun onDrawOver(canvas: Canvas, parent: RecyclerView, state: RecyclerView.State) {

        super.onDrawOver(canvas, parent, state)

        canvas.save()

        val lineHeight = mContext.resources.getDimensionPixelSize(R.dimen.dp_1)
        val interactLineHeight = mContext.resources.getDimensionPixelSize(R.dimen.dp_0_5)
        val itemCount = parent.childCount

        paint.apply {
            isAntiAlias = true
            style = Paint.Style.STROKE
            strokeWidth = lineHeight.toFloat()
            color = mContext.resources.getColor(R.color.purple_700)
            isDither = true
        }

        interactPaint.apply {
            isAntiAlias = true
            style = Paint.Style.STROKE
            strokeWidth = interactLineHeight.toFloat()
            color = mContext.resources.getColor(R.color.purple_700)
            isDither = true
        }

        when (mType) {
            TYPE_ONE -> {
                typeOne(canvas, parent, itemCount)
            }
            else -> {
                typeTwo(canvas, parent, itemCount)
            }
        }

        canvas.restore()
    }

    /*
    * 第一种样式
    * */
    private fun typeOne(canvas: Canvas, parent: RecyclerView, itemCount: Int) {
        for (i in 0.until(itemCount)) {

            val child = parent.getChildAt(i)
            val width = child.measuredWidth
            val height = child.measuredHeight

            val actualWidthX0 = width.times(i % mSpanCount)
            val actualWidthX1 = actualWidthX0 + width

            val actualHeightYa = height.times(i / mSpanCount)
            val actualHeightYb = actualHeightYa + height

            //左竖线
            canvas.drawRect(0F, 0F, 0F, actualHeightYb.toFloat(), paint)

            //上横线
            canvas.drawRect(
                actualWidthX0.toFloat(),
                0F,
                actualWidthX1.toFloat(),
                0F,
                paint
            )

            //右竖线
            val columnIndex = i % mSpanCount + 1
            val kPaint = if (columnIndex == mSpanCount) paint else interactPaint
            canvas.drawRect(
                actualWidthX1.toFloat(),
                0F,
                actualWidthX1.toFloat(),
                actualHeightYb.toFloat(),
                kPaint
            )

            //下横线
            val rowIndex = (i + mRowCount) / mRowCount
            val rPaint = if (rowIndex == mRowCount) paint else interactPaint
            canvas.drawRect(
                actualWidthX0.toFloat(),
                actualHeightYb.toFloat(),
                actualWidthX1.toFloat(),
                actualHeightYb.toFloat(),
                rPaint
            )
        }
    }

    /*
    * 第二种样式
    * */
    private fun typeTwo(canvas: Canvas, parent: RecyclerView, itemCount: Int) {
        val remainCount = itemCount % mSpanCount
        val modCount = itemCount / mSpanCount
        val condition = remainCount == 0
        if (condition) {
            typeOne(canvas, parent, itemCount)
        } else {
            if (modCount >= 1) {
                typeOne(canvas, parent, itemCount)
                //绘制剩余部分
                val child = parent.getChildAt(0)
                val width = child.measuredWidth
                val height = child.measuredHeight
                //补全剩余的横线
                canvas.drawRect(
                    width.times(remainCount).toFloat(),
                    height.times(modCount + 1).toFloat(),
                    width.times(mSpanCount).toFloat(),
                    height.times(modCount + 1).toFloat(),
                    paint
                )
                //补全剩余的竖线
                canvas.drawRect(
                    width.times(mSpanCount).toFloat(),
                    height.times(modCount).toFloat(),
                    width.times(mSpanCount).toFloat(),
                    height.times(modCount + 1).toFloat(),
                    paint
                )
            } else {
                typeOne(canvas, parent, itemCount)
            }
        }
    }

    companion object {
        const val TYPE_ONE = 0x01
        const val TYPE_TWO = 0x02
    }

}